package com.ycy.dubbo;

import com.ycy.dubbo.client.ExpService;
import com.ycy.dubbo.client.ThreadService;
import com.ycy.dubbo.client.TimeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;

@Slf4j
@Controller
public class HelloWordController {

  Logger logger = LoggerFactory.getLogger(this.getClass());

  @DubboReference(timeout = 1000, retries = 2, version = "1.1.0", check = false)
  TimeService timeService;

  @DubboReference(timeout = 1000, retries = 2, version = "1.1.0", check = false)
  ThreadService threadService;

  //  @DubboReference(timeout = 1000, retries = 2, version = "1.1.0")
  ExpService expService;

  @PostConstruct
  public void post() {
    log.info("init");
  }

  @RequestMapping(value = "getTime")
  @ResponseBody
  public String getTime() {
    String threadName = timeService.getTimeout();
    return threadName;
  }

  @RequestMapping(value = "getThreadName")
  @ResponseBody
  public String getThreadName() {
    String threadName = threadService.getThreadName();
    return threadName;
  }

  @GetMapping(value = "throwExp")
  @ResponseBody
  public int throwExp(@RequestParam String expType) {
    int i = 0;
    try {
      i = expService.throwExp(expType);
    } catch (Exception e) {
      logger.error("fail", e);
//      e.printStackTrace();
    }
    return i;
  }

}
